
class GameScene extends BaseScene {
    static GetPhyLen(len: number): number {
        return len / GameScene.PhyScale
    }
    static GetEgretLen(len: number): number {
        return len * GameScene.PhyScale
    }
    static GetPhyPos(x, y: number): Box2D.Common.Math.b2Vec2 {
        let ppos = new Box2D.Common.Math.b2Vec2()
        ppos.x = GameScene.GetPhyLen(x)
        ppos.y = GameScene.GetPhyLen(y)
        return ppos
    }
    static GetEgretPos(x, y: number): Box2D.Common.Math.b2Vec2 {
        let epos = new Box2D.Common.Math.b2Vec2()
        epos.x = GameScene.GetEgretLen(x)
        epos.y = GameScene.GetEgretLen(y)
        return epos
    }

    public static PhyScale: number = 30;
    public static PhyStep: number = 30;

    private _keyboard: KeyBoard = new KeyBoard()
    private _mainPlayer: CarObject = new CarObject(this)

    private _world: Box2D.Dynamics.b2World = new Box2D.Dynamics.b2World(new Box2D.Common.Math.b2Vec2(0, 0), true)
    private _debug: Box2D.Dynamics.b2DebugDraw = null
    private _timeFrame: number = 0

    private _leftJoyStick: JoyStick = null
    private _rightJoyStick: JoyStick = null
    public get LeftStickPercent(): number {
        return this._leftJoyStick.JoyRadiusPercent
    }
    public get RightStickPercent(): number {
        return this._rightJoyStick.JoyRadiusPercent
    }

    constructor() {
        super()     
    }

    public onEnter(): void {
        super.onEnter()

        this._leftJoyStick = new JoyStick()
        this._leftJoyStick.Init(LayerManager.UI_Game, 200, 400)
        this._leftJoyStick.VerticalJoy = true

        this._rightJoyStick = new JoyStick()
        this._rightJoyStick.Init(LayerManager.UI_Game, 900, 400)
        this._rightJoyStick.VerticalJoy = true

        this.addLayerAt(LayerManager.UI_Game, 10)
        this.addLayerAt(LayerManager.Game_Bg, 1)
        App.ViewManager.open(ViewConsts.GameMap)
        //App.ViewManager.open(ViewConsts.GameCalculate)

        this._keyboard.addEventListener(KeyBoard.onkeydown, this.onKeyDown, this)
        this._keyboard.addEventListener(KeyBoard.onkeyup, this.onKeyUp, this)

        this._mainPlayer.loadResource()
        this._mainPlayer.initPhy(this._world)
        App.ControllerManager.applyFunc(ControllerConsts.GameMap, GameMapConsts.SetMainPlayer, this._mainPlayer)
        App.ControllerManager.applyFunc(ControllerConsts.GameMap, GameMapConsts.LookAtPlayer)

        this.createDebug()
        App.ControllerManager.applyFunc(ControllerConsts.GameMap, GameMapConsts.SetDebugDisplay, this._debug)

    }

    public onExit(): void {
        super.onExit()

        this._keyboard.removeEventListener(KeyBoard.onkeydown, this.onKeyDown, this)
        this._keyboard.removeEventListener(KeyBoard.onkeyup, this.onKeyUp, this)
    }

    public enterFrame(time: number) {
        this._timeFrame++
        if (this._timeFrame % 2 == 0) {
            this._world.Step(1 / GameScene.PhyStep, 4, 6)
            this._world.DrawDebugData()
            //console.debug("===============> %d", time)

            this._mainPlayer.updateByPhy()
        }
    }

    public createDebug(): void {
        this._debug = new Box2D.Dynamics.b2DebugDraw();
        //this._debug.SetSprite(display);
        this._debug.SetDrawScale(GameScene.PhyScale);
        this._debug.SetLineThickness(1);
        this._debug.SetAlpha(0.8);
        this._debug.SetFillAlpha(0.5);
        this._debug.SetFlags(Box2D.Dynamics.b2DebugDraw.e_shapeBit);
        this._world.SetDebugDraw(this._debug);
    }

    private onKeyDown(evt: egret.Event) {
        console.debug("onKeyDown:" + evt.data)
        switch (evt.data[evt.data.length - 1]) {
            case "W": {
            } break
            case "S": {
            } break
            case "A": {
                this._mainPlayer.leftEngine = 0
            } break
            case "D": {
                this._mainPlayer.rightEngine = 0
            } break
            case "Q": {
                this._mainPlayer.leftEngine += 50
            } break
            case "E": {
                this._mainPlayer.rightEngine += 50
            } break
            case "Z": {
                this._mainPlayer.leftEngine -= 2
            } break
            case "C": {
                this._mainPlayer.rightEngine -= 2
            } break
        }
        evt.data = null
    }
    private onKeyUp(evt: egret.Event) {
        console.debug("onKeyUp:" + evt.data)
    }

}